package com.cyyang.shop.controller;

import java.io.File;
import java.util.UUID;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cyyang.shop.bean.Brand;
import com.cyyang.shop.bean.Spu;
import com.cyyang.shop.bean.SpuVo;
import com.cyyang.shop.service.BrandService;
import com.cyyang.shop.service.CatService;
import com.cyyang.shop.service.SpuService;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("spu")
public class SpuController {

	@Value("${uploadPath}")
	private String uploadPath;
	
	@Reference
	SpuService spuService;
	
	@Reference
	BrandService brandService;
	
	@Reference
	CatService catService;
	/**
	 * 跳转修改
	 */
	//spu/toUpdate/3
	@RequestMapping("toUpdate/{spuId}")
	public String toUpdate(Model m,@PathVariable("spuId")Integer spuId) {
		System.err.println(spuId);
		PageInfo<Brand> info1 = brandService.findBrand(1, Integer.MAX_VALUE, null);
		m.addAttribute("brands", info1.getList());
		//根据spuId查询spu用来修改的回显
		Spu spu = spuService.finSpuBySpuId(spuId);
		m.addAttribute("spu", spu);
		return "spu/update";
	}
	/**
	 * 添加spu
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public boolean add(Spu spu,MultipartFile picFileName) {
		try {
			if(spu!=null && spu.getId()!=null) {
				//修改
				//开始图片上传
				String picPath = upload(picFileName);
				if(picPath!=null) {
					spu.setSmallPic(picPath);
				}
				spuService.update(spu);
				return true;
			}
			String path = upload(picFileName);
			spu.setSmallPic(path);
			spuService.add(spu);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	//图片上传的方法
		private String upload(MultipartFile filePic) {
			String newName= null;
			if(filePic!=null && !filePic.isEmpty()) {
				String originalFilename = filePic.getOriginalFilename();//aaa.jpg
				//获取文件的后缀名
				String subName = originalFilename.substring(originalFilename.lastIndexOf("."));//.jpg
				//为该文件随机一个名称
				String preName = UUID.randomUUID().toString();//asdflasdkfjldskasdf
				//拼接新的文件名
				 newName = preName+subName;
				File dest = new File(uploadPath, newName);
				try {
					filePic.transferTo(dest);
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
			return newName;
		}
	
	@RequestMapping("getTree")
	@ResponseBody
	public Object getTree() {
		return catService.getTree(0);
	}
	
	@RequestMapping("toAdd")
	public String toAdd(Model m) {
		PageInfo<Brand> info1 = brandService.findBrand(1, Integer.MAX_VALUE, null);
		m.addAttribute("brands", info1.getList());
		return "spu/add";
	}
	
	@RequestMapping("list")
	public String list (SpuVo vo,Model m,@RequestParam(defaultValue="1")int pageNum,@RequestParam(defaultValue="5")int pageSize) {
		PageInfo<Spu> info = spuService.list(pageNum, pageSize,vo);
		PageInfo<Brand> info1 = brandService.findBrand(1, Integer.MAX_VALUE, null);
		m.addAttribute("brands", info1.getList());
		m.addAttribute("vo", vo);
		m.addAttribute("info", info);
		return "spu/list";
	}
	
	@RequestMapping("delSpuById")
	@ResponseBody
	public boolean delSpuById(@RequestParam("ids[]")Integer ...ids) {
		try {
			spuService.delSpuById(ids);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
